home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / blocks.c next >
Encoding:
C/C++ Source or Header  |  1996-11-16  |  1.8 KB  |  75 lines

  1. Article 12636 of comp.os.linux:
  2. Path: samba!concert!rutgers!sun-barr!olivea!uunet!world!jrs
  3. From: jrs@world.std.com (Rick Sladkey)
  4. Newsgroups: comp.os.linux
  5. Subject: finally, true disk usage (was: disk defragmentor)
  6. Message-ID: <JRS.92Oct7230625@lepton.world.std.com>
  7. Date: 8 Oct 92 03:06:25 GMT
  8. References: <1992Oct7.042114.11444@monu6.cc.monash.edu.au>
  9.     <1992Oct7.211109.15029@bernina.ethz.ch>
  10. Sender: jrs@world.std.com (Rick Sladkey)
  11. Organization: The Internet
  12. Lines: 58
  13. In-Reply-To: almesber@nessie.cs.id.ethz.ch's message of Wed, 7 Oct 1992 21:11:09 GMT
  14.  
  15. Werner's frag.c program gave me an idea.  Since du only reports sizes
  16. calculated from the file size, there is no way to find out how many
  17. true blocks a file uses short of deleting the file and watching df.
  18. Here is a variation on his program that counts true block usage, less
  19. indirect blocks.
  20. -----
  21. /* blocks.c - true block usage counter for linux - rick sladkey */
  22.  
  23. /* based on the program frag.c by Werner Almesberger */
  24.  
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <sys/stat.h>
  29.  
  30. #define FIBMAP 1
  31.  
  32. int main(int argc,char **argv)
  33. {
  34.     int i, j, block, blocks, fd;
  35.     struct stat st;
  36.  
  37.     if (argc == 1) {
  38.         fprintf(stderr,"usage: %s filename ...\n", argv[0]);
  39.         exit(1);
  40.     }
  41.     for (j = 1; j < argc; j++) {
  42.         if ((fd = open(argv[j],O_RDONLY)) < 0) {
  43.             perror(argv[j]);
  44.             exit(1);
  45.         }
  46.         if (fstat(fd,&st) < 0) {
  47.             perror(argv[j]);
  48.             exit(1);
  49.         }
  50.         if (!S_ISREG(st.st_mode)) {
  51.             fprintf(stderr, "%s: not a regular file\n", argv[j]);
  52.             close(fd);
  53.             continue;
  54.         }
  55.         blocks = 0;
  56.         for (i = 0; i < ((st.st_size + 1023) >> 10); i++) {
  57.             block = i;
  58.             if (ioctl(fd, FIBMAP, &block) < 0) {
  59.                 perror(argv[1]);
  60.                 exit(1);
  61.             }
  62.             if (block)
  63.                 blocks++;
  64.         }
  65.         printf("%d\t%s\n", blocks, argv[j]);
  66.         close(fd);
  67.     }
  68.     exit(0);
  69. }
  70. --
  71. Rick Sladkey
  72. jrs@world.std.com
  73.  
  74.  
  75.